home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Emacs / word.c < prev   
C/C++ Source or Header  |  1989-05-30  |  9KB  |  276 lines

  1. /* word.c */
  2.  
  3. /*
  4.  * The routines in this file implement commands that work word at a time.
  5.  * There are all sorts of word mode commands. If I do any sentence and/or
  6.  * paragraph mode commands, they are likely to be put in this file.
  7.  */
  8.  
  9. #include        <stdio.h>
  10. #include        "ed.h"
  11.  
  12.  
  13. /* Word wrap on n-spaces. Back-over whatever precedes the point on the current
  14.  * line and stop on the first word-break or the beginning of the line. If we
  15.  * reach the beginning of the line, jump back to the end of the word and start
  16.  * a new line.  Otherwise, break the line at the word-break, eat it, and jump
  17.  * back to the end of the word.
  18.  *      NOTE:  This function may leaving trailing blanks.
  19.  * Returns TRUE on success, FALSE on errors.
  20.  */
  21. wrapword(n)
  22. int n;
  23. {
  24.         register int cnt, oldp;
  25.         oldp = curwp->w_dotp;
  26.         cnt = -1;
  27.         do {                            
  28.                 cnt++;
  29.                 if (! backchar(NULL, 1))
  30.                         return(FALSE);
  31.         }
  32.         while (! inword());
  33.         if (! backword(NULL, 1))
  34.                 return(FALSE);
  35.         if (oldp == (int) (curwp->w_dotp && curwp->w_doto)) {
  36.                 if (! backdel(NULL, 1))
  37.                         return(FALSE);
  38.                 if (! newline(NULL, 1))
  39.                         return(FALSE);
  40.         }
  41.         return(forwword(NULL, 1) && forwchar(NULL, cnt));
  42. }
  43.                                 
  44. /*
  45.  * Move the cursor backward by "n" words. All of the details of motion are
  46.  * performed by the "backchar" and "forwchar" routines. Error if you try to
  47.  * move beyond the buffers.
  48.  */
  49. backword(f, n)
  50. {
  51.         if (n < 0)
  52.                 return (forwword(f, -n));
  53.         if (backchar(FALSE, 1) == FALSE)
  54.                 return (FALSE);
  55.         while (n--) {
  56.                 while (inword() == FALSE) {
  57.                         if (backchar(FALSE, 1) == FALSE)
  58.                                 return (FALSE);
  59.                 }
  60.                 while (inword() != FALSE) {
  61.                         if (backchar(FALSE, 1) == FALSE)
  62.                                 return (FALSE);
  63.                 }
  64.         }
  65.         return (forwchar(FALSE, 1));
  66. }
  67.  
  68. /*
  69.  * Move the cursor forward by the specified number of words. All of the motion
  70.  * is done by "forwchar". Error if you try and move beyond the buffer's end.
  71.  */
  72. forwword(f, n)
  73. {
  74.         if (n < 0)
  75.                 return (backword(f, -n));
  76.         while (n--) {
  77.                 while (inword() == FALSE) {
  78.                         if (forwchar(FALSE, 1) == FALSE)
  79.                                 return (FALSE);
  80.                 }
  81.                 while (inword() != FALSE) {
  82.                         if (forwchar(FALSE, 1) == FALSE)
  83.                                 return (FALSE);
  84.                 }
  85.         }
  86.         return (TRUE);
  87. }
  88.  
  89. /*
  90.  * Move the cursor forward by the specified number of words. As you move,
  91.  * convert any characters to upper case. Error if you try and move beyond the
  92.  * end of the buffer. Bound to "M-U".
  93.  */
  94. upperword(f, n)
  95. {
  96.         register int    c;
  97.  
  98.         if (n < 0)
  99.                 return (FALSE);
  100.         while (n--) {
  101.                 while (inword() == FALSE) {
  102.                         if (forwchar(FALSE, 1) == FALSE)
  103.                                 return (FALSE);
  104.                 }
  105.                 while (inword() != FALSE) {
  106.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  107.                         if (c>='a' && c<='z') {
  108.                                 c -= 'a'-'A';
  109.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  110.                                 lchange(WFHARD);
  111.                         }
  112.                         if (forwchar(FALSE, 1) == FALSE)
  113.                                 return (FALSE);
  114.                 }
  115.         }
  116.         return (TRUE);
  117. }
  118.  
  119. /*
  120.  * Move the cursor forward by the specified number of words. As you move
  121.  * convert characters to lower case. Error if you try and move over the end of
  122.  * the buffer. Bound to "M-L".
  123.  */
  124. lowerword(f, n)
  125. {
  126.         register int    c;
  127.  
  128.         if (n < 0)
  129.                 return (FALSE);
  130.         while (n--) {
  131.                 while (inword() == FALSE) {
  132.                         if (forwchar(FALSE, 1) == FALSE)
  133.                                 return (FALSE);
  134.                 }
  135.                 while (inword() != FALSE) {
  136.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  137.                         if (c>='A' && c<='Z') {
  138.                                 c += 'a'-'A';
  139.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  140.                                 lchange(WFHARD);
  141.                         }
  142.                         if (forwchar(FALSE, 1) == FALSE)
  143.                                 return (FALSE);
  144.                 }
  145.         }
  146.         return (TRUE);
  147. }
  148.  
  149. /*
  150.  * Move the cursor forward by the specified number of words. As you move
  151.  * convert the first character of the word to upper case, and subsequent
  152.  * characters to lower case. Error if you try and move past the end of the
  153.  * buffer. Bound to "M-C".
  154.  */
  155. capword(f, n)
  156. {
  157.         register int    c;
  158.  
  159.         if (n < 0)
  160.                 return (FALSE);
  161.         while (n--) {
  162.                 while (inword() == FALSE) {
  163.                         if (forwchar(FALSE, 1) == FALSE)
  164.                                 return (FALSE);
  165.                 }
  166.                 if (inword() != FALSE) {
  167.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  168.                         if (c>='a' && c<='z') {
  169.                                 c -= 'a'-'A';
  170.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  171.                                 lchange(WFHARD);
  172.                         }
  173.                         if (forwchar(FALSE, 1) == FALSE)
  174.                                 return (FALSE);
  175.                         while (inword() != FALSE) {
  176.                                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  177.                                 if (c>='A' && c<='Z') {
  178.                                         c += 'a'-'A';
  179.                                         lputc(curwp->w_dotp, curwp->w_doto, c);
  180.                                         lchange(WFHARD);
  181.                                 }
  182.                                 if (forwchar(FALSE, 1) == FALSE)
  183.                                         return (FALSE);
  184.                         }
  185.                 }
  186.         }
  187.         return (TRUE);
  188. }
  189.  
  190. /*
  191.  * Kill forward by "n" words. Remember the location of dot. Move forward by
  192.  * the right number of words. Put dot back where it was and issue the kill
  193.  * command for the right number of characters. Bound to "M-D".
  194.  */
  195. delfword(f, n)
  196. {
  197.         register int    size;
  198.         register LINE   *dotp;
  199.         register int    doto;
  200.  
  201.         if (n < 0)
  202.                 return (FALSE);
  203.         dotp = curwp->w_dotp;
  204.         doto = curwp->w_doto;
  205.         size = 0;
  206.         while (n--) {
  207.                 while (inword() == FALSE) {
  208.                         if (forwchar(FALSE, 1) == FALSE)
  209.                                 return (FALSE);
  210.                         ++size;
  211.                 }
  212.                 while (inword() != FALSE) {
  213.                         if (forwchar(FALSE, 1) == FALSE)
  214.                                 return (FALSE);
  215.                         ++size;
  216.                 }
  217.         }
  218.         curwp->w_dotp = dotp;
  219.         curwp->w_doto = doto;
  220.         return (ldelete(size, TRUE));
  221. }
  222.  
  223. /*
  224.  * Kill backwards by "n" words. Move backwards by the desired number of words,
  225.  * counting the characters. When dot is finally moved to its resting place,
  226.  * fire off the kill command. Bound to "M-Rubout" and to "M-Backspace".
  227.  */
  228. delbword(f, n)
  229. {
  230.         register int    size;
  231.  
  232.         if (n < 0)
  233.                 return (FALSE);
  234.         if (backchar(FALSE, 1) == FALSE)
  235.                 return (FALSE);
  236.         size = 0;
  237.         while (n--) {
  238.                 while (inword() == FALSE) {
  239.                         if (backchar(FALSE, 1) == FALSE)
  240.                                 return (FALSE);
  241.                         ++size;
  242.                 }
  243.                 while (inword() != FALSE) {
  244.                         if (backchar(FALSE, 1) == FALSE)
  245.                                 return (FALSE);
  246.                         ++size;
  247.                 }
  248.         }
  249.         if (forwchar(FALSE, 1) == FALSE)
  250.                 return (FALSE);
  251.         return (ldelete(size, TRUE));
  252. }
  253.  
  254. /*
  255.  * Return TRUE if the character at dot is a character that is considered to be
  256.  * part of a word. The word character list is hard coded. Should be setable.
  257.  */
  258. inword()
  259. {
  260.         register int    c;
  261.  
  262.         if (curwp->w_doto == llength(curwp->w_dotp))
  263.                 return (FALSE);
  264.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  265.         if (c>='a' && c<='z')
  266.                 return (TRUE);
  267.         if (c>='A' && c<='Z')
  268.                 return (TRUE);
  269.         if (c>='0' && c<='9')
  270.                 return (TRUE);
  271.         if (c=='$' || c=='_')                   /* For identifiers      */
  272.                 return (TRUE);
  273.         return (FALSE);
  274. }
  275.  
  276.